package com.castle.fortress.admin.member.member.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.member.member.entity.MemberEntity;
import com.castle.fortress.admin.member.member.dto.MemberDto;
import com.castle.fortress.admin.member.member.service.MemberService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import java.util.List;

/**
 * 会员表 控制器
 *
 * @author 
 * @since 2025-04-15
 */
@Api(tags="会员表管理控制器")
@Controller
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 页面
     * @return
     */
/*    @ApiIgnore
    @RequestMapping({"/page/member/member/member"})
    public String toPage(){
        return "admin/member/member/pages-member.html";
    }*/

    /**
     * 会员表的分页展示
     * @param memberDto 会员表实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("会员表分页展示")
    @GetMapping("/member/member/member/page")
    @ResponseBody
    @RequiresPermissions("member:member:member:pageList")
    public RespBody<IPage<MemberDto>> pageMember(MemberDto memberDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<MemberDto> page = new Page(pageIndex, pageSize);
        IPage<MemberDto> pages = memberService.pageMemberExtends(page, memberDto);

        return RespBody.data(pages);
    }

    /**
     * 会员表的列表展示
     * @param memberDto 会员表实体类
     * @return
     */
    @ApiOperation("会员表列表展示")
    @GetMapping("/member/member/member/list")
    @ResponseBody
    public RespBody<List<MemberDto>> listMember(MemberDto memberDto){
        List<MemberDto> list = memberService.listMember(memberDto);
        return RespBody.data(list);
    }

    /**
     * 会员表保存
     * @param memberDto 会员表实体类
     * @return
     */
    @ApiOperation("会员表保存")
    @PostMapping("/member/member/member/save")
    @ResponseBody
    @RequiresPermissions("member:member:member:save")
    public RespBody<String> saveMember(@RequestBody MemberDto memberDto){
        if(memberDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        MemberEntity memberEntity = ConvertUtil.transformObj(memberDto,MemberEntity.class);
        if(memberService.save(memberEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员表编辑
     * @param memberDto 会员表实体类
     * @return
     */
    @ApiOperation("会员表编辑")
    @PostMapping("/member/member/member/edit")
    @ResponseBody
    @RequiresPermissions("member:member:member:edit")
    public RespBody<String> updateMember(@RequestBody MemberDto memberDto){
        if(memberDto == null || memberDto.getId() == null || memberDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        MemberEntity memberEntity = ConvertUtil.transformObj(memberDto,MemberEntity.class);
        if(memberService.updateById(memberEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员表删除
     * @param id
     * @return
     */
    @ApiOperation("会员表删除")
    @PostMapping("/member/member/member/delete")
    @ResponseBody
    @RequiresPermissions("member:member:member:delete")
    public RespBody<String> deleteMember(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(memberService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 会员表批量删除
     * @param ids
     * @return
     */
    @ApiOperation("会员表批量删除")
    @PostMapping("/member/member/member/deleteBatch")
    @ResponseBody
    @RequiresPermissions("member:member:member:deleteBatch")
    public RespBody<String> deleteMemberBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(memberService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员表详情
     * @param id 会员表id
     * @return
     */
    @ApiOperation("会员表详情")
    @GetMapping("/member/member/member/info")
    @ResponseBody
    @RequiresPermissions("member:member:member:info")
    public RespBody<MemberDto> infoMember(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        MemberDto memberDto =  memberService.getByIdExtends(id);

        return RespBody.data(memberDto);
    }


}
